import {
  unboundPhonesByCategory
} from '@/api/business/staylnventory'
import {getInfo, listDict} from '@/api/business/gameDict.js'

export const myMixins = {
  data() {
    return {
      disabled: false
    }
  }, methods: {

    /** 修改按钮操作 */
    handleUpdate(row, type) {
      // 重置form
      this.title = type ? type : "修改";
      this.open = true;
      this.isTransactionComplete = false;
      this.$nextTick(() => {
        this.listDictData(row);
      });
    },

    listDictData(row) {
      // 游戏分类
      listDict({dictCodeId: row.dictCodeId}).then(response => {
        let data = response.data;
        let result = [];
        data.map(ii => {
          if (ii.gameKey === 'group') {
            this.regionalServers = ii.children;
          } else {
            result.push(ii);
          }
        });

        this.result = result;
        if (this.result.length > 0) {
          //处理多选框问题
          this.result.map((item, indx) => {
            if (item.fieldType == 'checkbox') {
              this.$set(row, 'checkbox_' + item.gameKey, new Array());
            }
          });

          // 其他赋值
          let bindingChannelId = ''
          let obj = {
            phoneCode: row.phoneCode, phoneId: row.phoneId, phoneNumber: row.phoneNumber
          };

          this.bindingChannelList.forEach(item => {
            if (item.dictValue == row.phoneField) {
              bindingChannelId = item.dictCode
            }
          });

          unboundPhonesByCategory(row.phoneField).then(res => {
            this.phoneList = res.data
            this.phoneData = obj
            if (!this.phoneList.find(item => item.phoneId == this.phoneData.phoneId)) {
              this.phoneList.unshift(this.phoneData)
            }
          });

          this.bindingChannelId = bindingChannelId;
          this.form = {
            checkList: [], ...row,
            bindingChannelId,
            realNameId: Number(row.realNameId),
            dictCodeId: Number(row.dictCodeId)
          };

          if (!this.form.imagess) this.form.imagess = [];
          if (!this.form.pxImagess) this.form.pxImagess = [];
          if (!this.form.pzImagess) this.form.pzImagess = [];

          // 动态赋值
          if (this.form.id && this.form.answer) {
            // 解析
            let row = JSON.parse(this.form.answer);

            // 单选框 radio
            let radio = row.radioSelections;
            if (JSON.stringify(radio) != '{}') {
              for (var key in radio) {
                this.$set(this.form, 'radioValues_' + key, radio[key])
              }
            }

            // 单选框 select
            let dropDown = row.dropDown;
            if (JSON.stringify(dropDown) != '{}') {
              for (var key in dropDown) {
                this.$set(this.form, 'dropDown_' + key, dropDown[key])
              }
            }

            // 输入框 input
            let inputValues = row.inputValues;
            if (JSON.stringify(inputValues) != '{}') {
              for (var key in inputValues) {
                this.$set(this.form, 'inputValues_' + key, inputValues[key])
              }
            }

            // 多选checkbox
            let checkObj = row.checkbox;
            this.result.map(i => {
              if (i.fieldType == 'checkbox') {
                if (checkObj) {
                  this.form['checkbox_' + i.gameKey] = checkObj['checkbox_' + i.gameKey];
                }
              }
            });
            this.$forceUpdate();
          }
        }
      });
    },

    copyPhone(phone) {
      // 复制到剪贴板
      if (phone) {
        const el = document.createElement('textarea');
        el.value = phone;
        document.body.appendChild(el);
        el.select();
        document.execCommand('copy');
        document.body.removeChild(el);
        // 通知用户已复制
        this.$message({
          message: '手机号已复制到剪贴板', type: 'success'
        });
      } else {
        this.$message.error('请先选择手机号！');
      }
    },

    //选中手机事件
    changePhone(value) {
      this.phoneList.forEach(item => {
        if (item.phoneId == value) {
          this.form.phoneCode = item.phoneCode
          this.form.phoneId = item.phoneId
        }
      })
    },

    // 添加待处理
    handleDealAccount(type) {
      this.result.map(item => {
        if (item.fieldType == 'checkbox' && item.children && item.children.length > 0) {
          let children = item.children;
          children.map(ii => {
            if (this.form[type] && this.form[type].indexOf(ii.gameField) > -1) {
              if (!this.form['checkbox_' + item.gameKey].includes(ii.id)) {
                this.form['checkbox_' + item.gameKey].push(ii.id)
              }
            }
          })
        }
      })
      this.$forceUpdate();
    },
  }
};
